package com.wxl.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wxl.entity.TOrder;
import com.wxl.mapper.TOrderMapper;
import com.wxl.service.TOrderService;
import com.wxl.service.SnowflakeService;
import com.wxl.util.SnowflakeIdWorker;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wuxl
 * @since 2025-05-15
 */
@Service
public class TOrderServiceImpl implements TOrderService {

    @Autowired
    private SnowflakeService snowflakeService;

    @Autowired
    private TOrderMapper tOrderMapper;


    public String createOrder(String product) {

        // 并发小时  都是偶数
        long orderId = snowflakeService.generateIdNoConCurr();

        String yyyymm = SnowflakeIdWorker.getYYYYMM(orderId);

        // 实际业务应保存到数据库
        TOrder entity = new TOrder();
        entity.setOrderId(orderId);
        entity.setMerchantId("mct001");
        entity.setUserId(81097864L);
        entity.setUserAddr("上海");
        entity.setPrice(10000);


        String order = String.format("TOrder{orderId=%d, product='%s', yyyymm=%s}", orderId, product,yyyymm);
        System.out.println(order);

        int insert = tOrderMapper.insert(entity);

        return order;
    }

    @Override
    public TOrder getOrder(Long orderId) {
        QueryWrapper<TOrder> wrapper = new QueryWrapper<>();
        wrapper.eq("order_id",orderId);
        return tOrderMapper.selectOne(wrapper);
    }
}
